var express = require("express");
var adminRouter = express.Router();
const admin = require("../dao/admin_dao");
const fs = require("fs");
const moment = require("moment")
const jwtUtil = require("../utils/jwtUtils");

// 中间件
adminRouter.use("/", async (req, res, next) => {
  // 登录接口不处理
  if (req.path === "/login") {
    return next();
  }
  const token = req.query.token || req.body.token;
  // 处理带有token的接口
  if (!!token) {
    const resData = await jwtUtil.verifysync(token, global.globalKey);
    if (resData.msg === "会话已过期") {
      return res.send({
        status: 401,
        statusText: "会话已过期,请重新登录!",
      });
    } else {
      return next();
    }
  }
  // 不带token的接口
  res.send({
    status: 401,
    statusText: "您暂时没有权限进行访问!",
  });
});

// 登录请求api
adminRouter.post("/login", (req, res) => {
  admin.login(req, res);
});

// 用户信息获取api
adminRouter.get("/getadminDataByToken", (req, res) => {
  admin.getAdminDataByToken(req, res);
});

// 获取标签分类
adminRouter.get("/getTagInfo", (req, res) => {
  admin.getTagInfo(req, res);
});

// 更新或发布文章
adminRouter.post("/updateArticle", (req, res) => {
  admin.updateArticle(req, res);
});

// 获取所有文章
adminRouter.get("/getArticleList", (req, res) => {
  admin.getArticleList(req, res);
});

// 删除文章
adminRouter.post("/delArticle", (req, res) => {
  admin.delArticle(req, res);
});

// 根据id查询文章
adminRouter.post("/getArticleById", (req, res) => {
  admin.getArticleById(req, res);
});

// 获取每种文章的数量
adminRouter.get("/getDiffTypeArticleInfo", (req, res) => {
  admin.getDiffTypeArticleInfo(req, res);
});


// 获取近七日的访客数
adminRouter.post("/getNearWeekVisitor", (req, res) => {
  admin.getNearWeekVisitor(req, res);
});

// 获取评论列表
adminRouter.get("/getCommentList", (req, res) => {
  admin.getCommentList(req, res);
});

// 审核评论状态
adminRouter.post("/changeCommentStatus", (req, res) => {
  admin.changeCommentStatus(req, res);
});

// 删除评论
adminRouter.post("/deleteCommentById", (req, res) => {
  admin.deleteCommentById(req, res);
});

//上传markdown图片
adminRouter.post("/uploadImage", (req, res) => {
  admin.uploadImage(req, res);
});

// 修改文章状态
adminRouter.post("/changeArticleStatus", (req, res) => {
  admin.changeArticleStatus(req, res);
});



module.exports = adminRouter;